package com.hospital.service.impl;

import com.alibaba.fastjson.JSON;
import com.hospital.controller.Code;
import com.hospital.controller.Result;
import com.hospital.dao.UserDao;
import com.hospital.domain.Department;
import com.hospital.domain.Doctor;
import com.hospital.domain.User;
import com.hospital.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;


    @Override
    public List<User> selectAll() {
        return userDao.selectAll();
    }

    @Override
    public List<Doctor> selectAllDoctor(String doctorName,int departmentID) {
        return userDao.selectAllDoctor(doctorName,departmentID);
    }

    @Override
    public Result login(String accountNum, String userPWD, HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        User user =  userDao.login(accountNum,userPWD);
        if(user != null){
            HttpSession session = request.getSession();
            session.setAttribute("userAccountNum",accountNum);
            session.setAttribute("userID",user.getUserID());
            return new Result(Code.GET_OK,user,"success");
        }
        else return new Result(Code.SYSTEM_ERR,user,"error");
    }


    @Override
    public List<Department> selectAllDep() {
        return userDao.selectAllDep();
    }

    @Override
    public int addUser(User user) {
//
//        String account = user.getAccountNum();
//
//        User user1 = userDao.selectByAccount(account);
//        System.out.println(user1);
//
//        if(user1 != null) return false;
//        else{
//            int n = userDao.addUser(user);
//            System.out.println(n);
//            return n != 0;
//        }
        return userDao.addUser(user);
    }

    @Override
    public User selectUser(int userID) {
        return userDao.selectUser(userID);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateUser(user);
    }

    @Override
    public User selectUserByAccount(String accountNum) {
        return userDao.selectUserByAccount(accountNum);
    }

}
